<?php
/**
 * CategoryController for managing admins user
 *
 * @author fusq
 */
require_once BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once BaseUtil::getModulePath("inventory")  .  '/forms/CategoryForm.php';
require_once BaseUtil::getModulePath("inventory")  .  '/services/CategoryService.php';

class Inventory_CategoryController  extends BaseController {

    public function init() {
        parent::init();
        $this->view->script = array('lib/dhtmlXCommon.js','lib/dhtmlXTree.js','lib/dhtmlXTreeExtend.js','inventory.js','lib/ui.dialog.js');
        $this->view->link=array('dhtmlXTree.css');
    }

    public function indexAction() {
        $categoryService = new CategoryService();
        $categoriesXml = $categoryService->getAllCategoriesXml();
        $this->view->categoryXml = $categoriesXml;
    }

    public function addAction() {
        $parentId = $this->_getParam("parentId");
        if(empty($parentId)){
           $parentId = -1;
        }
        $this->_helper->layout->disableLayout();
        $categoryService = new CategoryService();
        $metadataSets = $categoryService->getMetadataSet();
        $categoriesXml = $categoryService->getAllCategoriesXml();
        $categoryForm = new CategoryForm($metadataSets);
        $this->view->form = $categoryForm;
        $this->view->categoryXml = $categoriesXml;
        $this->view->parentId = $parentId;
    }

    public function subaddAction() {
        $categoryService = new CategoryService();
        $metadataSets = $categoryService->getMetadataSet();
        $form = new CategoryForm($metadataSets);
        if(!$form->isValid($_POST)) {
            $categoriesXml = $categoryService->getAllCategoriesXml();
            $this->view->categoryXml = $categoriesXml;
            $this->view->form = $form;
            return $this->render('add');
        }
        $category = array();
        $category['short_name'] = $form->getValue("shortName");
        $category['full_name'] = $form->getValue("fullName");
        $sortOrder = $form->getValue("sortOrder");
        if(!empty($sortOrder)) {
            $category['sort_order'] = $form->getValue("sortOrder");
        }
        $category['url_title'] = $form->getValue("urlTitle");
        $category['visible'] = $form->getValue("visible");
        $category['seo_title'] = $form->getValue("seoTitle");
        $category['seo_keyword'] = $form->getValue("seoKeyWord");
        $category['price_section'] = $form->getValue("priceSection");
        $category['seo_description'] = $form->getValue("seoDescription");
        $category['metadata_set_id'] = $form->getValue("metadataSet");
        $parentId = $form->getValue("parent");
        $category['parent_id'] = $parentId == "-1" ? "0" :  $parentId;
        $categoryService->addCategory($category);
        //$this->_redirector->gotoUrl('inventory/category');
        echo 'true';
        exit();
    }

    public function editAction() {
        $this->_helper->layout->disableLayout();
        $categoryId = $this->_getParam('id');
        $categoryService = new CategoryService();
        $metadataSets = $categoryService->getMetadataSet();
        $category = $categoryService->getCategoryById($categoryId);
        $categoriesXml = $categoryService->getAllCategoriesXml();
        $categoryForm = new CategoryForm($metadataSets,$category);
        $this->view->form = $categoryForm;
        $this->view->categoryXml = $categoriesXml;
        $this->view->id=$categoryId;
    }

    public function subeditAction() {
        $categoryService = new CategoryService();
        $category = array("id" => HttpUtil::getPostValue("categoryId"));
        $metadataSets = $categoryService->getMetadataSet();
        $form = new CategoryForm($metadataSets,$category);
        if(!$form->isValid($_POST)) {
            $categoriesXml = $categoryService->getAllCategoriesXml();
            $this->view->categoryXml = $categoriesXml;
            $this->view->form = $form;
            return $this->render('edit');
        }
        $category = array();
        $categoryId = $form->getValue("categoryId");
        $category['short_name'] = $form->getValue("shortName");
        $category['full_name'] = $form->getValue("fullName");
        $sortOrder = $form->getValue("sortOrder");
        if(!empty($sortOrder)) {
            $category['sort_order'] = $form->getValue("sortOrder");
        }
        $category['url_title'] = $form->getValue("urlTitle");
        $category['visible'] = $form->getValue("visible");
        $category['seo_title'] = $form->getValue("seoTitle");
        $category['seo_keyword'] = $form->getValue("seoKeyWord");
        $category['price_section'] = $form->getValue("priceSection");
        $category['seo_description'] = $form->getValue("seoDescription");
        $category['metadata_set_id'] = $form->getValue("metadataSet");
        $parentId = $form->getValue("parent");
        $category['parent_id'] = $parentId == "-1" ? "0" :  $parentId;
        $categoryService->updateCategory($categoryId,$category);
        //$this->_redirector->gotoUrl('inventory/category');
        echo 'true';
        exit();
    }

    public function deleteAction() {
        $categoryService = new CategoryService();
        $categoryId = $this->_getParam('id');
        $category = array();
        $category['is_delete'] = 1;
        $categoryService->updateCategory($categoryId,$category);
        $this->_redirector->gotoUrl('inventory/category');
    }

    public function testAction(){
        
    }
}